'use strict'

###*
 # @ngdoc directive
 # @name xoceanApp.directive:templateBox
 # @description
 # # templateBox
 # used in '/#/wizard/step_1_1'
###
angular.module('xoceanApp')
  .directive('templateBox', ($compile, Template, ngDialog, $state, $rootScope)->
    restrict: 'E'
    scope: true
    link: (scope, element, attrs) ->
      return if not scope.html
      element.html('')
      element.append $compile(scope.html)(scope)

      scope.$parent.saveCustomTmpl = ->
        scope.$parent.$parent.hasEditedTmpl = true
        _tmpl = scope.selectedTmpl
        if _tmpl
          _tmpl.html = element.html()
          delete _tmpl.id
          Template.saveCustom(_tmpl).then ->
            console.log 'done!'
            scope.$parent.$parent.html = _tmpl.html
            $state.go('xocean.wizard.basicInfo')
        else
          scope.$parent.$parent.html = element.html()
          $state.go('xocean.wizard.basicInfo')
      return
  )
